﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class AdminRole
    {
        private readonly KeRui.DAL.AdminRole dal = new KeRui.DAL.AdminRole();

        public int Add(KeRui.Model.AdminRole model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.AdminRole> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.AdminRole> list = new List<KeRui.Model.AdminRole>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.AdminRole item = new KeRui.Model.AdminRole();
                    if (dt.Rows[i]["RoleID"].ToString() != "")
                    {
                        item.RoleID = int.Parse(dt.Rows[i]["RoleID"].ToString());
                    }
                    item.RoleName = dt.Rows[i]["RoleName"].ToString();
                    item.RoleFlag = dt.Rows[i]["RoleFlag"].ToString();
                    if (dt.Rows[i]["AdminNum"].ToString() != "")
                    {
                        item.AdminNum = int.Parse(dt.Rows[i]["AdminNum"].ToString());
                    }
                    if (dt.Rows[i]["OrderID"].ToString() != "")
                    {
                        item.OrderID = int.Parse(dt.Rows[i]["OrderID"].ToString());
                    }
                    item.Note = dt.Rows[i]["Note"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int RoleID)
        {
            return this.dal.Delete(RoleID);
        }

        public List<KeRui.Model.AdminRole> GetList()
        {
            DataSet set = new KeRui.DAL.Common().GetList("AdminRole", -1, -1, "", "OrderID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.AdminRole> GetList(string RoleIDs)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            string strWhere = "";
            if (RoleIDs == "")
            {
                strWhere = " 1=2";
            }
            else
            {
                strWhere = "RoleID in (" + RoleIDs + ")";
            }
            DataSet set = common.GetList("AdminRole", -1, -1, strWhere, "OrderID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public int GetMaxOrderID()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetMaxOrderID(" AdminRole ", " OrderID ");
        }

        public KeRui.Model.AdminRole GetModel(int RoleID)
        {
            return this.dal.GetModel(RoleID);
        }

        public KeRui.Model.AdminRole GetModelByCache(int RoleID)
        {
            string cacheKey = "AdminRoleModel-" + RoleID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(RoleID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.AdminRole) cache;
        }

        public int Update(KeRui.Model.AdminRole model)
        {
            return this.dal.Update(model);
        }

        public int UpdateRoleFlagForSiteFlag(KeRui.Model.AdminRole model)
        {
            return this.dal.UpdateRoleFlagForSiteFlag(model);
        }
    }
}

